/**
 * 
 */
package userController;

import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.sql.Time;
import java.util.Date;
import java.util.ResourceBundle;

import reservationController.ReservationController;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import userEntity.CustomerBookingDisplayable;
import userEntity.MembershipEntity;
import userLogic.CustomerLogic;
import vehicleController.ViewVehicleController;
import vehicleEntity.SaleVehicleEntity;

/**
 * @author Yokesh Kumar
 *
 */
public class CustomerController implements Initializable{
	@FXML
	private Label userName;
	@FXML
	private Button reservation;
	@FXML
	private Button cancelReservation;
	@FXML
	private Button saleList;
	@FXML
	private Button bookingHistory;
	@FXML
	private Button membership;
	@FXML
	private StackPane customerStackPane;
	@FXML
	private TableColumn<CustomerBookingDisplayable, Integer> reserveId;
	@FXML
	private TableColumn<CustomerBookingDisplayable, Date> reserveDate;
	@FXML
	private TableColumn<CustomerBookingDisplayable, String> status;
	@FXML
	private TableColumn<CustomerBookingDisplayable, Date> rentDate;
	@FXML
	private TableColumn<CustomerBookingDisplayable, Time> rentTime;
	@FXML
	private TableColumn<CustomerBookingDisplayable, Date> returnDate;
	@FXML
	private TableColumn<CustomerBookingDisplayable, Time> returnTime;
	@FXML
	private TableColumn<CustomerBookingDisplayable, String> vehicle;
	@FXML
	private TableColumn<CustomerBookingDisplayable, Double> amount;
	@FXML
	private TableView<CustomerBookingDisplayable> bookingTable;
	
	
	private static ObservableList<CustomerBookingDisplayable> custDispList;
	
	private Stage custStage;
	
	
	
	/**
	 * @param customerStackPane the customerStackPane to set
	 */
	public void setCustomerStackPane(Node node) {
		customerStackPane.getChildren().setAll(node);
	}

	public void setName(String name){
		//userName = new Label(name);
		userName.setText(name);
	}
	
	public void SetStage(Stage Stage)
	{
		this.custStage=Stage;
	}
	
	/**
	 * Invoke reservationcontroller.doReservation
	 * @param event
	 */
	@FXML
	public void doReservation(ActionEvent event){
		ReservationController.page = 0;
		Navigator.loadStackPane(Navigator.ReservationInitialFXML);
	}
	
	/**
	 * Invoke reservationcontroller.cancelreservation
	 * @param event
	 */
	@FXML
	public void cancelReservation(ActionEvent event){
		ReservationController rc = new ReservationController();
		rc.cancelReservation();
	}
	
	/**
	 * Invoke User controller.viewSaleList method
	 * @param event
	 */
	@FXML
	public void viewSaleList(ActionEvent event){
		ViewVehicleController viewVehicleControl =  new ViewVehicleController();
		viewVehicleControl.viewSaleList();
		
	}
	
	
	@FXML
	public void viewBookingHistory(ActionEvent event){
		CustomerLogic logic = new CustomerLogic();
		custDispList = logic.fetchCustomerBookingDetails();
		Navigator.loadStackPane(Navigator.BOOKING_HISTORY_FXML);
		
	}
	
	@FXML
	public void viewMembership(ActionEvent event) throws IOException{
		CustomerLogic logic = new CustomerLogic();
		MembershipEntity membership = logic.fetchMembershipDetails(Navigator.getPhoneNumber());
		if(membership == null){
			Navigator.loadStackPane(Navigator.BecomeAMemberFXML);
		}
		else{
			MembershipController membershipControl = new MembershipController();
			membershipControl.showMembershipDetails(membership);
		}
	}
	
	/**
	 * UserController.editProfile() method
	 * @param event
	 * @throws SQLException 
	 */
	@FXML
	public void editProfile(ActionEvent event){
		UserController user = new UserController();
		user.editProfile(Navigator.getPhoneNumber());

	}
	
	@FXML
	public void contactUs(ActionEvent event){
		 Navigator.loadStackPane(Navigator.contactUsFXML);
	}
	
	@FXML
	public void viewFaq(ActionEvent event){
		Navigator.loadStackPane(Navigator.FAQFXML);
	}
	
	/**
	 * load usercontroller.changePassword method
	 * @param event
	 */
	@FXML
	public void changePassword(ActionEvent event){
		UserController managerobjchange = new UserController();
		managerobjchange.changePassword();
	}
	
	@FXML
	public void home(ActionEvent event){
		Navigator.loadStackPane(Navigator.CUSTOMER_HOME);
	}
	
	@FXML
	public void logOut(ActionEvent event) throws IOException{
		//Navigator.setMembership(null);
		Navigator.setCustomer(null);
		Navigator.setPhoneNumber(null);
		 FXMLLoader myLoader = new  FXMLLoader(getClass().getResource("/userManagementUI/LoginPage.fxml"));
	        Stage MainStage = new Stage();
	        Pane MainPagePane = (Pane)myLoader.load();
	        LoginController controller = (LoginController) myLoader.getController();
	        controller.SetStage(MainStage);
	        Scene myScene = new Scene(MainPagePane);        
	        MainStage.setScene(myScene);
	        MainStage.setTitle("SUPERRENT");
	        MainStage.setResizable(false);
	        MainStage.setWidth(605);
	        MainStage.setHeight(445);
	        MainStage.show();
	        custStage.close();
	}

	/* (non-Javadoc)
	 * @see javafx.fxml.Initializable#initialize(java.net.URL, java.util.ResourceBundle)
	 */
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		if(custDispList != null){
			System.out.println("Inside initializable");
			reserveId.setCellValueFactory(new PropertyValueFactory<CustomerBookingDisplayable, Integer>("reservationId"));
			reserveDate.setCellValueFactory(new PropertyValueFactory<CustomerBookingDisplayable, Date>("reservationDate"));
			status.setCellValueFactory(new PropertyValueFactory<CustomerBookingDisplayable, String>("status"));
			rentDate.setCellValueFactory(new PropertyValueFactory<CustomerBookingDisplayable, Date>("rentDate"));
			rentTime.setCellValueFactory(new PropertyValueFactory<CustomerBookingDisplayable, Time>("rentTime"));
			returnDate.setCellValueFactory(new PropertyValueFactory<CustomerBookingDisplayable, Date>("returnDate"));
			returnTime.setCellValueFactory(new PropertyValueFactory<CustomerBookingDisplayable, Time>("returnTime"));
			vehicle.setCellValueFactory(new PropertyValueFactory<CustomerBookingDisplayable, String>("vehicleName"));
			amount.setCellValueFactory(new PropertyValueFactory<CustomerBookingDisplayable, Double>("finalCost"));
			bookingTable.getItems().setAll(custDispList);
			custDispList = null;
		}
		
	}

	
	
	/*public void undobuttons(){
		reservation.setDefaultButton(false);
		cancelReservation.setDefaultButton(false);
		saleList.setDefaultButton(false);
		bookingHistory.setDefaultButton(false);
		membership.setDefaultButton(false);
	}*/
}
